<?php
require_once("includes/pagetop.php");

if(isset($_SESSION['Allyfolio_User_ID']) && $_SESSION['Allyfolio_User_ID']!="")
{
	if(isset($_REQUEST["dna_purpose"]))
	{
		if($_REQUEST["dna_purpose"]=="about")
		{
			$qryUpdate="update af_users set About_You=:About_You where User_ID='".$_SESSION['Allyfolio_User_ID']."'";
			$pdodbobj->query($qryUpdate,array(":About_You"=>$_REQUEST["About_You"] ));
			echo "SUCCESS";
		}
		else if( $_REQUEST["dna_purpose"]=="quotes")
		{
			$qryUpdate="update af_users set Quotes=:Quotes where User_ID='".$_SESSION['Allyfolio_User_ID']."'";
			$pdodbobj->query($qryUpdate,array(":Quotes"=>$_REQUEST["Quotes"] ));
			echo "SUCCESS";
		}
		else if( $_REQUEST["dna_purpose"]=="education")
		{
			$qryUpdate="update af_users set Certification=:Certification, Research=:Research,Post_Graduate=:Post_Graduate,Graduate=:Graduate,Intermediate=:Intermediate, High_School=:High_School where User_ID='".$_SESSION['Allyfolio_User_ID']."'";
			$pdodbobj->query($qryUpdate,array(":Certification"=>$_REQUEST["Certification"], ":Research"=>$_REQUEST["Research"], ":Post_Graduate"=>$_REQUEST["Post_Graduate"], ":Graduate"=>$_REQUEST["Graduate"], ":Intermediate"=>$_REQUEST["Intermediate"], ":High_School"=>$_REQUEST["High_School"] ));
			echo "SUCCESS";
		}
		else if( $_REQUEST["dna_purpose"]=="work")
		{
			$qryUpdate="update af_users set Profession=:Profession, Time_Period=:Time_Period,Designation=:Designation, Company_Name=:Company_Name where User_ID='".$_SESSION['Allyfolio_User_ID']."'";
			$pdodbobj->query($qryUpdate,array(":Profession"=>$_REQUEST["Profession"], ":Time_Period"=>$_REQUEST["Time_Period"], ":Designation"=>$_REQUEST["Designation"], ":Company_Name"=>$_REQUEST["Company_Name"] ));
			echo "SUCCESS";
		}
		else if( $_REQUEST["dna_purpose"]=="basic")
		{
			$dobArr=explode("/",$_REQUEST["DOB"]);
			
			if(count($dobArr)==3)
			{
				$dob=mktime(0,0,1,$dobArr[1],$dobArr[0],$dobArr[2]);
				$qryUpdate="update af_users set Gender=:Gender, DOB=:DOB,Relationship=:Relationship,Language=:Language,Family=:Family, Religious_View=:Religious_View,Political_View=:Political_View, Sports=:Sports where User_ID='".$_SESSION['Allyfolio_User_ID']."'";
				$pdodbobj->query($qryUpdate,array(":Gender"=>$_REQUEST["Gender"], ":DOB"=>$dob, ":Relationship"=>$_REQUEST["Relationship"], ":Language"=>$_REQUEST["Language"], ":Family"=>$_REQUEST["Family"], ":Religious_View"=>$_REQUEST["Religious_View"], ":Political_View"=>$_REQUEST["Political_View"], ":Sports"=>$_REQUEST["Sports"] ));
				echo "SUCCESS";
			}
			else
			{
				echo "ERROR|~|Please select correct date of birth.";
			}
								
			
		}
		else if( $_REQUEST["dna_purpose"]=="contact")
		{
			$latlngArr=get_lat_long($_REQUEST["Address"]);
										
			$qryUpdate="update af_users set  Mobile_Number=:Mobile_Number,Phone_Number=:Phone_Number,Fax_Number=:Fax_Number,Address=:Address,Latitude='".$latlngArr["lat"]."', Longitude='".$latlngArr["lng"]."',  Residence=:Residence,Country_ID=:Country_ID, Website_Link=:Website_Link where User_ID='".$_SESSION['Allyfolio_User_ID']."'";
			$pdodbobj->query($qryUpdate,array(":Mobile_Number"=>$_REQUEST["Mobile_Number"], ":Phone_Number"=>$_REQUEST["Phone_Number"], ":Fax_Number"=>$_REQUEST["Fax_Number"], ":Address"=>$_REQUEST["Address"], ":Residence"=>$_REQUEST["Residence"], ":Country_ID"=>$_REQUEST["Country_ID"], ":Website_Link"=>$_REQUEST["Website_Link"] ));
			
			//echo $qryUpdate="update af_users set  Mobile_Number='".$_REQUEST["Mobile_Number"]."',Phone_Number='".$_REQUEST["Phone_Number"]."',Fax_Number='".$_REQUEST["Fax_Number"]."',Address='".$_REQUEST["Address"]."',Latitude='".$latlngArr["lat"]."', Longitude='".$latlngArr["lng"]."',  Residence='".$_REQUEST["Residence"]."',Country_ID='".$_REQUEST["Country_ID"]."', Website_Link='".$_REQUEST["Website_Link"]."' where User_ID='".$_SESSION['Allyfolio_User_ID']."'";		
			//$pdodbobj->query($qryUpdate,array());
			echo "SUCCESS";
		}
		else if( $_REQUEST["dna_purpose"]=="notebook")
		{
			$qryUpdate="update af_users set Notebook=:Notebook where User_ID='".$_SESSION['Allyfolio_User_ID']."'";
			$pdodbobj->query($qryUpdate,array(":Notebook"=>$_REQUEST["Notebook"] ));
			echo "SUCCESS";
		}
	}
	else if(isset($_REQUEST["purpose"]) && $_REQUEST["purpose"]=="pick")
	{
		//check isPageOwner
		$qryIsPageOwner="select * from af_company_page where Comp_Page_ID=:Comp_Page_ID and User_ID=:User_ID";
		$condition[":User_ID"]=$_SESSION['Allyfolio_User_ID'];
		$condition[":Comp_Page_ID"]=$_REQUEST["Comp_Page_ID"];
		$arrIsPageOwner=$pdodbobj->fetch_array($pdodbobj->query($qryIsPageOwner,$condition));
		if(count($arrIsPageOwner)>0)
		{
			echo "ERROR|~|You can not pick your own page.";
		}
		else
		{
			//Check Already Picked
			$qryCheckCurrentPicked="select * from af_company_page_members where comp_page_id=:comp_page_id and user_id=:user_id";
			$condition=array();
			$condition[":user_id"]=$_SESSION['Allyfolio_User_ID'];
			$condition[":comp_page_id"]=$_REQUEST["Comp_Page_ID"];
			$arrCheckCurrentPicked=$pdodbobj->fetch_array($pdodbobj->query($qryCheckCurrentPicked,$condition));
			if(count($arrCheckCurrentPicked)>0) //already picked
			{
				$qryDeletePicked="delete from af_company_page_members where comp_page_id=:comp_page_id and user_id=:user_id";
				$pdodbobj->query($qryDeletePicked,$condition);
				echo "SUCCESS|~|Pick";
			}
			else //Not Picked
			{
				$qryInsertPicked="insert into af_company_page_members set comp_page_id=:comp_page_id, user_id=:user_id, join_date='".time()."'";
				$pdodbobj->query($qryInsertPicked,$condition);
				echo "SUCCESS|~|Unpick";
			}
		}
	}
	else if(isset($_REQUEST["purpose"]) && $_REQUEST["purpose"]=="blogpick")
	{
		//Check Already Picked
		$qryCheckCurrentPicked="select * from  af_blog_pick where blog_id=:blog_id and user_id=:user_id";
		$condition=array();
		$condition[":user_id"]=$_SESSION['Allyfolio_User_ID'];
		$condition[":blog_id"]=$_REQUEST["blog_id"];
		$arrCheckCurrentPicked=$pdodbobj->fetch_array($pdodbobj->query($qryCheckCurrentPicked,$condition));
		if(count($arrCheckCurrentPicked)>0) //already picked
		{
			$qryDeletePicked="delete from af_blog_pick where blog_id=:blog_id and user_id=:user_id";
			$pdodbobj->query($qryDeletePicked,$condition);
			echo "SUCCESS|~|Pick";
		}
		else //Not Picked
		{
			$qryInsertPicked="insert into af_blog_pick set blog_id=:blog_id, user_id=:user_id, pick_date='".time()."'";
			$pdodbobj->query($qryInsertPicked,$condition);
			echo "SUCCESS|~|Unpick";
		}
		
		//Get Pick Count
		$qryBlogPickCount="select count(*) as countPickCount from af_blog_pick,af_users where blog_id=:blog_id and af_users.Activation_Status='Active' and af_blog_pick.user_id=af_users.User_ID "; 
		$condition=array();
		$condition[":blog_id"]=$_REQUEST["blog_id"];
		$arrBlogPickCount=$pdodbobj->fetch_array($pdodbobj->query($qryBlogPickCount,$condition));
		if(is_array($arrBlogPickCount) && $arrBlogPickCount[0]["countPickCount"]>0)
		{
			echo "|~|".$arrBlogPickCount[0]["countPickCount"];		
		}
		else
		{
			echo "|~|"."0";		
		}
	}
	else if(isset($_REQUEST["purpose"]) && $_REQUEST["purpose"]=="blog_page_follow")
	{
		//Check Already Followed
		$qryCheckCurrentFollowed="select * from  af_blog_page_follow where blog_page_id=:blog_page_id and user_id=:user_id";
		$condition=array();
		$condition[":user_id"]=$_SESSION['Allyfolio_User_ID'];
		$condition[":blog_page_id"]=$_REQUEST["blog_page_id"];
		$arrCheckCurrentFollowed=$pdodbobj->fetch_array($pdodbobj->query($qryCheckCurrentFollowed,$condition));
		
		if(count($arrCheckCurrentFollowed)>0) //already Followed
		{
			$qryDeletePicked="delete from af_blog_page_follow where blog_page_id=:blog_page_id and user_id=:user_id";
			$pdodbobj->query($qryDeletePicked,$condition);
			echo "SUCCESS|~|Follow";
		}
		else //Not Picked
		{
			$qryInsertPicked="insert into af_blog_page_follow set blog_page_id=:blog_page_id, user_id=:user_id, follow_date='".time()."'";
			$pdodbobj->query($qryInsertPicked,$condition);
			echo "SUCCESS|~|UnFollow";
		}
	}
	else
	{
		echo "ERROR|~|Looks like there is some problem in modification. Please try after some time.";
	}
}
?>
<?php
function get_lat_long($adrs)
{
	$latlngArr="";
	$latlngArr["lat"]=0;
	$latlngArr["lng"]=0;
	$adrs=str_replace(" ", "+", $adrs);	
	$str_add=file_get_contents("http://maps.google.com/maps/api/geocode/xml?address=".$adrs."&sensor=false");
	$latlngArr["full_str"]=$str_add;
	
	$array_lat=explode("<lat>", $str_add);
	if(count($array_lat)>1)	
	{
		$pos_lat=strpos($array_lat[1], "</lat>");
		$lat=substr($array_lat[1], 0, $pos_lat);	
		$latlngArr["lat"]=$lat;	
	}
	$array_lng=explode("<lng>", $str_add);	
	if(count($array_lng)>1)	
	{
		$pos_lng=strpos($array_lng[1], "</lng>");
		$lng=substr($array_lng[1], 0, $pos_lng);
		$latlngArr["lng"]=$lng;
	}
	
	return $latlngArr;
}
?>